using wt.wine.Application.Common.Interfaces;
using wt.wine.Infrastructure.Persistence;
using wt.wine.Infrastructure.Services;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Data;
using Npgsql;
using Microsoft.EntityFrameworkCore;
using wt.wine.Infrastructure.Persistence.Repository;
using wt.wine.Infrastructure.InternalHttp;
using wt.wine.Domain.Options;

namespace wt.wine.Infrastructure
{
    public static class DependencyInjection
    {
        public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
        {

            services.AddSingleton(configuration);

            services.Configure<InternalHttpOption>(configuration.GetSection("InternalHttpService"));
            services.AddScoped<IInternalHttpService, InternalHttpService>();
            services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(configuration.GetConnectionString("pgsql")).UseLowerCaseNamingConvention());

            services.AddTransient<IDbConnection>(sp => new NpgsqlConnection(configuration.GetConnectionString("pgsql")));
            services.AddScoped<IApplicationDbContext>(provider => provider.GetService<ApplicationDbContext>());
            services.AddScoped<IDomainEventService, DomainEventService>();         
            services.AddTransient<IDateTime, DateTimeService>();
            services.AddScoped<IUsertableRepository, UsertableRepository>();
            services.AddScoped<IDevicetableRepository, DevicetableRepository>();
            services.AddScoped<IGetDevocetableStatusRepository, GetDevocetableStatusRepository>();
            services.AddScoped<IordertableRepository, ordertableRepository>();
            services.AddScoped<IskutableRepository, skutableRepository>();
            services.AddScoped<IDevicerecordRepository, DevicerecordRepository>();
            services.AddScoped<ICheckWineResponse, CheckWineResponse>();
            services.AddScoped<IConfigtableRepository, ConfigtableRepository>();
            services.AddScoped<IRoletableRepository, RoletableRepository>();
            services.AddScoped<IMenuTableRepository, MenuTableRepository>();
            services.AddScoped<IUserdevicetableRepository, UserdevicetableRepository>();
            services.AddSingleton<ICurrentUserService, CurrentUserService>();
            services.AddSingleton<ITokenUserService, TokenUserService>();
            return services;
        }
    }
}